//
//  XCSMSAttributeTouchLabel.m
//  HOLLASMSLoginKit
//
//  Created by Mars on 2019/10/10.
//  Copyright © 2019 mao PengLin. All rights reserved.
//

#import "XCSMSAttributeTouchLabel.h"
//#import "UIColor+XCSMSColor.h"
#import "XCSMSTheme.h"
#import "Config.h"

@interface XCSMSAttributeTouchLabel()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;
@end

@implementation XCSMSAttributeTouchLabel

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        _textView = [[UITextView alloc]initWithFrame:self.bounds];
        _textView.backgroundColor = [UIColor clearColor];
        _textView.delegate = self;
        _textView.editable = NO;//必须禁止输入，否则点击将会弹出输入键盘
        _textView.scrollEnabled = NO;//可选的，视具体情况而定
        _textView.font = MediumFont(11);
        _textView.linkTextAttributes = @{NSForegroundColorAttributeName:[XCSMSTheme sharedTheme].placeholderTextColor};
        
        UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longBangClick:)];
        [_textView addGestureRecognizer:longPress];
        
        [self addSubview:_textView];
    }
    return self;
    
}

- (void)setContent:(NSString *)content WithHeightlightTexts:(NSArray<NSString *> *)heightlightTexts {
    
    _content = content;

    if (heightlightTexts.count > 1){
        _heightlightText = heightlightTexts.firstObject;
        _heightlightText2 = heightlightTexts.lastObject;
    }else{
        _heightlightText = heightlightTexts.firstObject;
    }
    
    UIFont *font = MediumFont(11);
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentCenter;
    NSDictionary *textAttributes = @{
                                     NSFontAttributeName: font,
                                     NSForegroundColorAttributeName: [XCSMSTheme sharedTheme].placeholderTextColor,
                                     NSParagraphStyleAttributeName: paragraph,
                                     };
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:content attributes:textAttributes];
    
    NSRange serviceRange = [content rangeOfString:_heightlightText];
    [attStr addAttribute:NSLinkAttributeName value:@"click://" range:serviceRange];
    [attStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:serviceRange];
    
    if (_heightlightText2){
        NSRange serviceRange2 = [content rangeOfString:_heightlightText2];
        [attStr addAttribute:NSLinkAttributeName value:@"click2://" range:serviceRange2];
        [attStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:serviceRange2];
    }
    _textView.attributedText = attStr;
    
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
    if ([[URL scheme] isEqualToString:@"click"]) {
        //        NSAttributedString *abStr = [textView.attributedText attributedSubstringFromRange:characterRange];
        if (self.touchBlock){
            self.touchBlock();
        }
        return NO;
    }
    
    if ([[URL scheme] isEqualToString:@"click2"]) {
        //        NSAttributedString *abStr = [textView.attributedText attributedSubstringFromRange:characterRange];
        if (self.touchBlock2){
            self.touchBlock2();
        }
        return NO;
    }
    
    return YES;
}

- (void)longBangClick:(UILongPressGestureRecognizer *)gesture{
    
}

@end
